package com.ssy.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssy.bus.mapper.DrugInfoMapper;
import com.ssy.bus.mapper.OrderMapper;
import com.ssy.bus.mapper.ProductionMapper;
import com.ssy.bus.pojo.DrugInfo;
import com.ssy.bus.pojo.Order;
import com.ssy.bus.pojo.Production;
import com.ssy.bus.service.IOrderService;
import com.ssy.bus.vo.OrderVo;
import com.ssy.common.CodeMsg;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author ssy
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DrugInfoMapper drugInfoMapper;

    @Autowired
    private ProductionMapper productionMapper;

    @Override
    public PageResult loadAllOrderByPage(OrderVo orderVo) {
        Page<Order> page = new Page<>(orderVo.getPage(), orderVo.getLimit());
        IPage<Order> orderIPage = this.orderMapper.selectPage(page, new LambdaQueryWrapper<Order>()
                .eq(Order::getPurId, orderVo.getPurId()));
        List<Order> orders = orderIPage.getRecords();
        for (Order order : orders) {
            //获取药品ID
            Integer drugId = order.getDrugId();
            DrugInfo drugInfo = this.drugInfoMapper.selectById(drugId);
            order.setDrugName(drugInfo.getName());
            order.setCompany(drugInfo.getCompany());
            //获取生产商ID
            Integer productionId = drugInfo.getProduction();
            Production production = this.productionMapper.selectById(productionId);
            order.setProductionName(production.getName());
        }
        return new PageResult(orderIPage.getTotal(), orders);
    }

    @Override
    public Result addMakeDrugInfoOne(Order order) {
        if (order == null) {
            return new Result(CodeMsg.ADD_ERROR);
        }
        Integer[] ids = order.getIds();
        for (Integer id : ids) {
            order.setDrugId(id);
            this.orderMapper.insert(order);
        }
        return new Result(CodeMsg.ADD_SUCCESS);
    }

    @Override
    public Result addMakeDrugInfoTwo(Order order) {
        if (order == null) {
            return new Result(CodeMsg.BATCH_SETTING_ERROR);
        }

        //设置药品规格
        String specifications = order.getNumber() + "g";
        order.setSpecifications(specifications);
        //设置药品批发额
        BigDecimal number = new BigDecimal("" + order.getNumber());
        BigDecimal wholesaleAmount = number.multiply(order.getTradePrice());
        order.setWholesaleAmount(wholesaleAmount);

        this.orderMapper.updateOrderByDrugId(order);
        return new Result(CodeMsg.BATCH_SETTING_SUCCESS);
    }

    @Override
    public Result deleteDrugInfoByDrugId(Integer id, String purId) {
        int i = this.orderMapper.delete(new LambdaQueryWrapper<Order>()
                .eq(Order::getDrugId, id)
                .eq(Order::getPurId, purId));
        return i > 0 ? new Result(CodeMsg.DELETE_SUCCESS) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result updateDrugInfoByDrugId(Order order) {
        if (order == null) {
            return new Result(CodeMsg.UPDATE_ERROR);
        }

        if (order.getNumber() != null && order.getTradePrice() != null) {
            //设置药品规格
            String specifications = order.getNumber() + "g";
            order.setSpecifications(specifications);
            //设置药品批发额
            BigDecimal number = new BigDecimal("" + order.getNumber());
            BigDecimal wholesaleAmount = number.multiply(order.getTradePrice());
            order.setWholesaleAmount(wholesaleAmount);
        }

        int i = this.orderMapper.update(order, new LambdaQueryWrapper<Order>()
                .eq(Order::getDrugId, order.getDrugId())
                .eq(Order::getPurId, order.getPurId()));
        return i > 0 ? new Result(CodeMsg.UPDATE_SUCCESS) : new Result(CodeMsg.UPDATE_ERROR);
    }

    @Override
    public Result selectCountWholesaleAmount(String purId) {
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.select("ifnull(sum(wholesale_amount),0) as sumWholesaleAmount");
        qw.eq("pur_id", purId);
        Order order = this.orderMapper.selectOne(qw);
        return new Result(order.getSumWholesaleAmount());
    }

}
